﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Drawing;

public partial class admin_UserList : System.Web.UI.Page
{
    DataSet usuEmployee = null, usuClient = null, usuVendor = null, usuGuest = null, usuInactive= null;
    rUser ruser = null;

    protected void Page_Load(object sender, EventArgs e)
    {
       if (Session["userid"] == null)  { Response.Redirect("../Default.aspx");    }
       else
        {
            restricted();
           permiso();
            ruser = (rUser)Session["userid"];



            if (!Page.IsPostBack)
            {
                usuEmployee =(DataSet)(Session["usuEmployee"]);
                usuClient = (DataSet)(Session["usuClient"]);
                usuVendor = (DataSet)(Session["usuVendor"]);
                usuGuest = (DataSet)(Session["usuInv"]);
                usuInactive = (DataSet)(Session["usuInactive"]);
                if (usuEmployee != null)
                {
                    gvuser.DataSource = usuEmployee.Tables["Tabla"];
                    gvuser.DataBind();
                }
                if (usuClient != null)
                {
                    gvclient.DataSource = usuClient.Tables["Tabla"];
                    gvclient.DataBind();
                }
                if (usuVendor != null)
                {
                    gvvendor.DataSource = usuVendor.Tables["Tabla"];
                    gvvendor.DataBind();
                }
                if (usuGuest != null)
                {
                    gvguest.DataSource = usuGuest.Tables["Tabla"];
                    gvguest.DataBind();
                }
                if (usuInactive != null){
                    gv_inactive.DataSource = usuInactive.Tables["Tabla"];
                    gv_inactive.DataBind();
                }
            }
            else
            {
                ruser = (rUser)Session["userid"];
            }
        }



    }

    protected void gvuser_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        ruser = (rUser)Session["userid"];

        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[7].Style.Add("display", "none"); // Hide
            e.Row.Cells[8].Style.Add("display", "none"); // Hide
            e.Row.Cells[9].Style.Add("display", "none"); // Hide
            e.Row.Cells[10].Style.Add("display", "none"); // Hide
        }

        if (e.Row.RowType == DataControlRowType.Header)
        {
            
            e.Row.CssClass = "letraNegra13";

            e.Row.Cells[2].Text = "Firstname";
            e.Row.Cells[2].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[3].Text = "Lastname";
            e.Row.Cells[3].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[4].Text = "User Id";
            e.Row.Cells[4].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Left;
            
            e.Row.Cells[5].Text = "Group";
            e.Row.Cells[5].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[6].Text = "Profile";
            e.Row.Cells[6].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[7].Text = "Alias";
            e.Row.Cells[7].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[7].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[8].Text = "Company";
            e.Row.Cells[8].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[8].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[9].Text = "Email";
            e.Row.Cells[9].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[9].HorizontalAlign = HorizontalAlign.Left;


            for (int i = 0; i < 10; i++)
            {
                if (e.Row.Cells[i].Text.Equals("&nbsp;"))
                {
                    e.Row.Cells[i].Text = "";
                }
                e.Row.Cells[i].Text = e.Row.Cells[i].Text.ToUpper();
            }

            for (int k = 0; k < 10; k++)
            {
                e.Row.Cells[k].HorizontalAlign = HorizontalAlign.Left;
            }
            e.Row.HorizontalAlign = HorizontalAlign.Left;

        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 0; i < 10; i++)
            {
                e.Row.Cells[i].CssClass="letraNegra13";
                if (e.Row.Cells[i].Text.Equals("&nbsp;"))
                {
                    e.Row.Cells[i].Text = "";
                }
            }
            e.Row.Cells[5].Text = e.Row.Cells[5].Text.ToUpper();
            e.Row.Cells[6].Text = e.Row.Cells[6].Text.ToUpper();

            string aux = "";
            aux = e.Row.Cells[2].Text;
            e.Row.Cells[2].Text = e.Row.Cells[4].Text;
            e.Row.Cells[4].Text = aux;
        }
    }

    protected void gvuser_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        usuEmployee = (DataSet)(Session["usuEmployee"]);
        if (usuEmployee != null)
        {
            gvuser.PageIndex = e.NewPageIndex;
            gvuser.DataSource = usuEmployee.Tables["Tabla"];
            gvuser.DataBind();
        }
    }
    
    protected void gvclient_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        usuClient = (DataSet)(Session["usuClient"]);
        if (usuClient != null)
        {
            gvclient.PageIndex = e.NewPageIndex;
            gvclient.DataSource = usuClient.Tables["Tabla"];
            gvclient.DataBind();
        }
    }

    public void restricted()
    {
        long permiso = 0;
        rUser user = (rUser)(Session["userid"]);
        if (user != null)
        {
           // permiso = long.Parse(user.getRestricted().ToString());
            RE_GenericBean permi = Utility.getPermisobyRuser(user, 14);
            permiso = permi.int64C2;
        }
        if ((permiso & 1) == 1) { Response.Redirect(Request.UrlReferrer.ToString()); }
    }

    public void permiso()
    {
        long permiso = 0;
        rUser user = (rUser)(Session["userid"]);
        if (user != null)
        {
           // permiso = long.Parse(user.getPermisoUser().ToString());
            RE_GenericBean permi = Utility.getPermisobyRuser(user, 14);
            permiso = permi.int64C1;
        }
        if ((permiso & 1) != 1) { gvuser.Enabled = false; gvclient.Enabled = false; gvguest.Enabled = false; }
    }
    protected void gvvendor_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
    }
    protected void gvguest_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        usuGuest = (DataSet)(Session["usuInv"]);
        if (usuGuest != null)
        {
            gvguest.PageIndex = e.NewPageIndex;
            gvguest.DataSource = usuGuest.Tables["Tabla"];
            gvguest.DataBind();
        }
    }
    protected void gvvendor_PageIndexChanging1(object sender, GridViewPageEventArgs e)
    {
        usuVendor = (DataSet)(Session["usuVendor"]);
        if (usuVendor != null)
        {
            gvvendor.PageIndex = e.NewPageIndex;
            gvvendor.DataSource = usuVendor.Tables["Tabla"];
            gvvendor.DataBind();
        }
    }

    
    protected void gvvendor_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        ruser = (rUser)Session["userid"];

        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[7].Style.Add("display", "none"); // Hide
            e.Row.Cells[8].Style.Add("display", "none"); // Hide
            e.Row.Cells[9].Style.Add("display", "none"); // Hide
            e.Row.Cells[10].Style.Add("display", "none"); // Hide
        }

        if (e.Row.RowType == DataControlRowType.Header)
        {

            e.Row.CssClass = "letraNegra13";

            e.Row.Cells[2].Text = "Firstname";
            e.Row.Cells[2].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[3].Text = "Lastname";
            e.Row.Cells[3].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[4].Text = "User Id";
            e.Row.Cells[4].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[5].Text = "Group";
            e.Row.Cells[5].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[6].Text = "Profile";
            e.Row.Cells[6].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[7].Text = "Alias";
            e.Row.Cells[7].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[7].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[8].Text = "Company";
            e.Row.Cells[8].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[8].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[9].Text = "Email";
            e.Row.Cells[9].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[9].HorizontalAlign = HorizontalAlign.Left;


            for (int i = 0; i < 10; i++)
            {
                if (e.Row.Cells[i].Text.Equals("&nbsp;"))
                {
                    e.Row.Cells[i].Text = "";
                }
                e.Row.Cells[i].Text = e.Row.Cells[i].Text.ToUpper();
            }

            for (int k = 0; k < 10; k++)
            {
                e.Row.Cells[k].HorizontalAlign = HorizontalAlign.Left;
            }
            e.Row.HorizontalAlign = HorizontalAlign.Left;

        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 0; i < 10; i++)
            {
                e.Row.Cells[i].CssClass = "letraNegra13";
                if (e.Row.Cells[i].Text.Equals("&nbsp;"))
                {
                    e.Row.Cells[i].Text = "";
                }
            }
            e.Row.Cells[5].Text = e.Row.Cells[5].Text.ToUpper();
            e.Row.Cells[6].Text = e.Row.Cells[6].Text.ToUpper();

            string aux = "";
            aux = e.Row.Cells[2].Text;
            e.Row.Cells[2].Text = e.Row.Cells[4].Text;
            e.Row.Cells[4].Text = aux;

            e.Row.Cells[5].Text = "VENDOR";
        }
    }
    protected void gv_inactive_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        ruser = (rUser)Session["userid"];

        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[7].Style.Add("display", "none"); // Hide
            e.Row.Cells[8].Style.Add("display", "none"); // Hide
            e.Row.Cells[9].Style.Add("display", "none"); // Hide
            e.Row.Cells[10].Style.Add("display", "none"); // Hide
        }

        if (e.Row.RowType == DataControlRowType.Header)
        {

            e.Row.CssClass = "letraNegra13";

            e.Row.Cells[2].Text = "Firstname";
            e.Row.Cells[2].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[3].Text = "Lastname";
            e.Row.Cells[3].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[4].Text = "User Id";
            e.Row.Cells[4].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[5].Text = "Group";
            e.Row.Cells[5].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[6].Text = "Profile";
            e.Row.Cells[6].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[7].Text = "Alias";
            e.Row.Cells[7].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[7].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[8].Text = "Company";
            e.Row.Cells[8].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[8].HorizontalAlign = HorizontalAlign.Left;

            e.Row.Cells[9].Text = "Email";
            e.Row.Cells[9].Style.Add("style", "text-decoration:none; ");
            e.Row.Cells[9].HorizontalAlign = HorizontalAlign.Left;


            for (int i = 0; i < 10; i++)
            {
                if (e.Row.Cells[i].Text.Equals("&nbsp;"))
                {
                    e.Row.Cells[i].Text = "";
                }
                e.Row.Cells[i].Text = e.Row.Cells[i].Text.ToUpper();
            }

            for (int k = 0; k < 10; k++)
            {
                e.Row.Cells[k].HorizontalAlign = HorizontalAlign.Left;
            }
            e.Row.HorizontalAlign = HorizontalAlign.Left;

        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 0; i < 10; i++)
            {
                e.Row.Cells[i].CssClass = "letraNegra13";
                if (e.Row.Cells[i].Text.Equals("&nbsp;"))
                {
                    e.Row.Cells[i].Text = "";
                }
            }
            e.Row.Cells[5].Text = e.Row.Cells[5].Text.ToUpper();
            e.Row.Cells[6].Text = e.Row.Cells[6].Text.ToUpper();

            string aux = "";
            aux = e.Row.Cells[2].Text;
            e.Row.Cells[2].Text = e.Row.Cells[4].Text;
            e.Row.Cells[4].Text = aux;

            //e.Row.Cells[5].Text = "VENDOR";
        }
    }
    protected void gv_inactive_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        
    }
    protected void gv_inactive_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        usuInactive = (DataSet)(Session["usuInactive"]);
        if (usuInactive != null)
        {
            gv_inactive.PageIndex = e.NewPageIndex;
            gv_inactive.DataSource = usuInactive.Tables["Tabla"];
            gv_inactive.DataBind();
        }
    }
}